﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.News
{
    public partial class NewsManage : HFController.BagePages
    {
        //新闻列表
        public List<hf.Model.News> list;
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员是否登录
            this.checkAdminIsLogin();

            //绑定新闻列表
            this.BindNews(0 ,"");

            if (!Page.IsPostBack)
            {
                //绑定分类选择框
                this.BindClass();

                string Action = this.GetQuery("action");
                switch (Action)
                {
                    case "delete": //单个删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        NewsController.getController().doDelegeNews(ID);
                        break;
                    case "deleteall": //批量删除
                        string IDS = this.GetAll("cid1");
                        NewsController.getController().doDelegeNews(IDS.Split(','));
                        break;
                    case "search": //快速搜索
                        this.doSearch(this.GetQuery("typeid"), this.GetQuery("classid"));
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定分类选择框
        /// </summary>
        private void BindClass()
        {
            //法律事务
            this.dropClassID.DataSource = LegalAffairsController.getController().getLegalAffairsList(1000);
            this.dropClassID.DataTextField = "LegalAffairsTitle";
            this.dropClassID.DataValueField = "ID";
            this.dropClassID.DataBind(); //执行绑定
            this.dropClassID.Items.Insert(0, new ListItem("--请选择--", "0"));

            //关联的法律事务
            this.dropClassID2.DataSource = LegalAffairsController.getController().getLegalAffairsList(1000);
            this.dropClassID2.DataTextField = "LegalAffairsTitle";
            this.dropClassID2.DataValueField = "ID";
            this.dropClassID2.DataBind(); //执行绑定
            this.dropClassID2.Items.Insert(0, new ListItem("--请选择--", "0"));

            //关联法律事务大类
            this.dropbClassID.Items.Insert(0, new ListItem("--请选择--", "0"));


            //新闻分类
            dropTopicID.DataSource = NewsController.getController().getTopicList(2000);
            dropTopicID.DataTextField = "TopicName";
            dropTopicID.DataValueField = "ID";
            dropTopicID.DataBind();
            dropTopicID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 关联小分类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropClassID2_SelectedIndexChanged(object sender, EventArgs e)
        {
            dropbClassID.DataSource = LegalAffairsController.getController().getLegalAffairsBiglClass(2000, "LegalAffairsID=" + this.dropClassID2.SelectedValue);//绑定数据源（下拉框支持List<>类型直接绑定）
            dropbClassID.DataValueField = "ID"; //下拉列表的值，用他写到数据库
            dropbClassID.DataTextField = "ClassName"; //下拉列表的显示文字，和要绑定的数据库字段一致
            dropbClassID.DataBind(); //执行绑定

            dropbClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定新闻列表
        /// </summary>
        private void BindNews(int TypeID ,string StrWhere)
        {
            string pagestr;
            if (TypeID == 0)
            {
                list = NewsController.getController().getNewsList(out pagestr);
            }
            else {
                list = NewsController.getController().getNewsList(StrWhere, out pagestr);
            }
            pager.Text = pagestr;
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="TypeID"></param>
        /// <param name="Key"></param>
        private void doSearch(string TypeID, string Key)
        {
            string strWhere = "";
            switch(TypeID)
            {
                case "1":
                    strWhere = "ClassID=" + Key;
                    break;
                case "2":
                    strWhere = "bClassID=" + Key;
                    break;
                case "3":
                    strWhere = "TopicID=" + Key;
                    break;
            }
            this.BindNews(1, strWhere);
        }

        /// <summary>
        /// 法律事务搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsManage.aspx?action=search&typeid=1&classid=" + dropClassID.SelectedValue);
        }

        /// <summary>
        /// 法律事务小类搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsManage.aspx?action=search&typeid=2&classid=" + dropbClassID.SelectedValue);
        }

        /// <summary>
        /// 新闻专题搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsManage.aspx?action=search&typeid=3&classid=" + dropTopicID.SelectedValue);
        }



        
    }
}